package com.lili.dt;

/**
 * @Auther: 李 力
 * @Date: 2024/8/9
 * @Description: 最长的公共字串长度
 * @version: 1.0
 */
public class CommonStr {

    //求长度
    public static int getCommonStr(String a, String b) {
        int max = 0;
        int[][] dp = new int[a.length()][b.length()];
        for (int i = 0; i < a.length(); i++) {
            for (int j = 0; j < b.length(); j++) {
                if (a.charAt(i) == b.charAt(j)) {
                    if (i == 0 || j == 0) {
                        dp[i][j] = 1;
                    } else {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                    }
                    max = Math.max(max, dp[i][j]);
                } else {
                    dp[i][j] = 0;
                }
            }
        }
        return max;
    }
}
